package top.aicey.ai.sparring.listener;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Splitter;

import lombok.extern.slf4j.Slf4j;
import top.aicey.ai.sparring.common.constants.AIConstants;
import top.aicey.ai.sparring.dto.event.GenerateReportEvent;
import top.aicey.ai.sparring.dto.llm.EvaluateScoreInfo;
import top.aicey.ai.sparring.entity.Scene;
import top.aicey.ai.sparring.llm.ICoolLLMService;
import top.aicey.ai.sparring.service.ISceneInstanceEvaluationService;
import top.aicey.ai.sparring.service.ISceneInstanceLogService;
import top.aicey.ai.sparring.service.ISceneService;

/**
 * 报告生成消息
 */
@Slf4j
@Component
public class ReportListener{

    @Autowired
    private ISceneInstanceLogService sceneInstanceLogService;

    @Autowired
    private ICoolLLMService coolLLMService;

    @Autowired
    private ISceneInstanceEvaluationService sceneInstanceEvaluationService;

    @Autowired
    private ISceneService sceneService;

    @Autowired
    private ReportEventPublisher reportEventPublisher;

    /**
     * 评估消息处理
     *
     * @param event 为将来扩展使用，目前返回Null
     * @return
     */
    public ReportHandlerStatsEnum handle(GenerateReportEvent event) {
        log.info("report message received: message={}", event);

        if (Objects.isNull(event)){
            log.info("event is null：messageId:{}",event);
            return ReportHandlerStatsEnum.FAIL;
        }


        // 开始集成用户的问答对，将其组织成内容
        String moduleId = event.getModuleId();
        String instanceId = event.getInstanceId();
        Long sceneId = event.getSceneId();
        // 判断是否已经结束问答了
        boolean idCompleted  = sceneInstanceEvaluationService.completeByInstanceId(moduleId,instanceId);

        if (idCompleted){
            log.info("event is completed");
            return ReportHandlerStatsEnum.FAIL;
        }


        String content = sceneInstanceLogService.generateAIRequestContent(moduleId,sceneId,instanceId);

        if (Objects.isNull(content)){
            Scene scene = sceneService.getById(sceneId);
            if (Objects.isNull(scene)){
                log.info("not exists trainingScene");
                return ReportHandlerStatsEnum.FAIL;
            }
            // 如果内容为空就没必要请求了 直接按照模板生成失败回复
            sceneInstanceEvaluationService.updateEvaluateScoreInfoToFail(event.getInstanceId(),scene);
            reportEventPublisher.reportCompleted(moduleId,sceneId,instanceId);
            return ReportHandlerStatsEnum.FAIL;
        }


        if (StringUtils.isEmpty(content)){
            return ReportHandlerStatsEnum.FAIL;
        }

        Scene scene = sceneService.getById(sceneId);

        if (Objects.isNull(scene)){
            log.info("not exists trainingScene");
            return ReportHandlerStatsEnum.FAIL;
        }

        // 将其推送给大模型获取五维评价分，并获取评语
        EvaluateScoreInfo evaluateScoreInfo = coolLLMService
            .generateEvaluateScoreInfo(content,
                Splitter.on(AIConstants.DIMENSIONS_SPLIT).trimResults().splitToList(scene.getDimensions()), scene.getCustomPrompt(), 100);

        // 生成模型信息失败
        if (Objects.isNull(evaluateScoreInfo)){
            log.info("not exists evaluateScoreInfo");
            return ReportHandlerStatsEnum.FAIL;
        }

        // 将其存储下来
        sceneInstanceEvaluationService.updateEvaluateScoreInfoToComplete(event.getInstanceId(),evaluateScoreInfo);
        reportEventPublisher.reportCompleted(moduleId,sceneId,instanceId);
        // 将json保存
        log.info("evaluateScoreInfo:{}",evaluateScoreInfo);
        return ReportHandlerStatsEnum.SUCCESS;
    }

}
